package com.link.base.base.projectmanage.service;

import com.link.base.base.common.model.Attachment;
import com.link.base.base.common.service.AttachmentService;
import com.link.base.base.projectmanage.dao.mybatis.mapper.TaskInfoMapper;
import com.link.base.base.projectmanage.model.TaskInfo;
import com.link.base.base.projectmanage.model.TaskVisitor;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 拜访信息
 *
 * @author 刘启鑫
 * @date 2020-09-11 16:52:46
 */
@Service
public class TaskInfoServiceImpl extends BasicServiceImpl<TaskInfo> implements TaskInfoService {

    @Resource
    private TaskInfoMapper taskInfoMapper;

    @Resource
    private TaskVisitorService taskVisitorService;

    @Resource
    private AttachmentService attachmentService;

    @Override
    public BasicMapper<TaskInfo> getBasicMapper() {
        return taskInfoMapper;
    }


    @Override
    public void beforDelete(TaskInfo entity) throws Exception {
        super.beforDelete(entity);
        entity = taskInfoMapper.queryTreeById(entity);
        if (!Objects.isNull(entity)) {
            List<TaskVisitor> taskVisitorList = entity.getTaskVisitors();
            List<Attachment> attachmentList = entity.getAttachments();
            if (!CollectionUtils.isEmpty(taskVisitorList)) {
                for (TaskVisitor taskVisitor : taskVisitorList) {
                    taskVisitorService.deleteById(taskVisitor);
                }
            }
            if (!CollectionUtils.isEmpty(attachmentList)) {
                for (Attachment attachment: attachmentList) {
                    attachmentService.deleteById(attachment);
                }
            }
        }
    }

    /**
     * 查询登录用户指定时间区间内，所有车型登记拜访计划
     * @return
     * @throws Exception
     */
    @Override
    public List<TaskInfo> queryAllByDateRange(TaskInfo entity) throws Exception {
        //获取查询月份de 最后一天
        Date firstDay = DateUtil.DATETIME_FORMATTER.parse(entity.getAttr1());
        Date lastDay = getLastDayOfMonth(firstDay);
        //查询时间有交集的数据
        //只要start_date>查询结束时间，则本条数据与当前查询时间范围肯定无交集
        //只要end_date<查询开始时间，则本条数据与当前查询时间范围肯定无交集 取反
        TaskInfo querVisit = new TaskInfo();
        querVisit.setAttr1("queryPrport");
        querVisit.setEstStartDate(entity.getAttr1());
        querVisit.setEstEndDate(DateUtil.DATETIME_FORMATTER.format(lastDay));
        //前端传递登录用户ID到attr2
        querVisit.setAttr2(entity.getAttr2());
        List<TaskInfo> taskInfos = taskInfoMapper.queryAll(querVisit);
        //处理数据 setname 把每条记录放入数组，再放入json对象{ result：[]}
        List<TaskInfo> resultArray = new ArrayList<>();
        for (TaskInfo taskInfo : taskInfos) {
            Date estStartDate = DateUtil.DATETIME_FORMATTER.parse(taskInfo.getEstStartDate());
            Date estEndDate = DateUtil.DATETIME_FORMATTER.parse(taskInfo.getEstEndDate());
            //需要裁减。如果预计开始日期小于当月第一天，赋值当月第一天，如果预计结束日期大于当月最后一天，赋值当月最后一天
            //预计开始时间
            if (estStartDate.before(firstDay)) {
                taskInfo.setEstStartDate(DateUtil.DATETIME_FORMATTER.format(firstDay));
            } else {
                taskInfo.setEstStartDate(DateUtil.DATETIME_FORMATTER.format(estStartDate));
            }
            //预计结束时间
            if (estEndDate.after(lastDay)) {
                taskInfo.setEstEndDate(DateUtil.DATETIME_FORMATTER.format(lastDay));
            } else {
                taskInfo.setEstEndDate(DateUtil.DATETIME_FORMATTER.format(estEndDate));
            }
            resultArray.add(taskInfo);
        }
        return resultArray;
    }


    /**
     * 获取给定日期的当月最后一天
     * @param firstDay
     * @return
     */
    public Date getLastDayOfMonth(Date firstDay) {
        //根据当月第一天，获取当月最后一天
        Calendar cal = Calendar.getInstance();
        cal.setTime(firstDay);
        //获取指定日期月份最大天数
        int lastday = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置月份中最后一天最后一秒
        cal.set(Calendar.DAY_OF_MONTH,lastday);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    @Override
    public List<TaskInfo> queryAllByHeadId(TaskInfo entity) {
        return taskInfoMapper.queryAllByHeadId(entity);
    }
}
